<template>
  <div>
    <van-nav-bar title="标题" fixed placeholder />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        name="form.mobile"
        label="用户名"
        placeholder="手机号"
        :rules="[
          {
            required: true,
            message: '请输入正确的手机号',
            pattern: /^1[3-9]\d{9}$/,
            trigger: 'onBlur'
          }
        ]"
      />
      <van-field
        v-model="form.code"
        type="password"
        name="form.code"
        label="验证码"
        placeholder="验证码"
        :rules="[
          {
            required: true,
            message: '请输入验证码',
            trigger: 'onBlur',
            pattern: /^\d{6}$/
          }
        ]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" :disabled="disabled" :loading="disabled" loading-text="加载中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { getTokenAPI } from '@/api'
import { setToken } from '@/utils/token'
export default {
  data () {
    return {
      form: {
        mobile: '15888888888',
        code: '246810'
      },
      disabled: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.disabled = true
      try {
        const res = await getTokenAPI({ ...this.form })
        // console.log(res)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '登录成功' })
        this.disabled = false
        this.$router.push('/Layout')
      } catch (err) {
        this.disabled = false
        Notify({ type: 'danger', message: '登陆失败' })
        return console.error(err)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
